package com.qwz.example;

import com.qwz.example.state.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Qiuwuzhi
 * @version V1.0
 * @Description: 投票管理
 * @Package com.qwz.example.VoteManager
 * @date 2019/12/25 17:52
 */
public class VoteManager {
    /**
     * 持有状态处理对象
     */
    private VoteState state;
    /**
     * 记录投票用户的结果，key为用户名，value为投票选项
     */
    private Map<String, String> mapVote = new HashMap<String, String>();
    /**
     * 记录投票用户的结果，key为用户名，value为投票次数
     */
    private Map<String, Integer> mapVoteCount = new HashMap<String, Integer>();

    /**
     * 获取记录用户投票结果的map
     *
     * @return
     */
    public Map<String, String> getMapVote() {
        return mapVote;
    }

    /**
     * 投票
     *
     * @param user
     * @param voteItem
     */
    public void vote(String user, String voteItem) {
        Integer oldVoteCount = mapVoteCount.get(user);
        if (oldVoteCount == null) {
            oldVoteCount = 0;
        }
        oldVoteCount += 1;
        mapVoteCount.put(user, oldVoteCount);
        if (oldVoteCount == 1) {
            state = new NormalVoteState();
        } else if (oldVoteCount > 1 && oldVoteCount < 5) {
            state = new RepeatVoteState();
        } else if (oldVoteCount >= 5 && oldVoteCount < 8) {
            state = new SpiteVoteState();
        } else if (oldVoteCount >= 8) {
            state = new BlackVoteState();
        }
        state.vote(user, voteItem, this);
    }
}
